package io.github.jyrmc.deploy.task

import io.github.jyrmc.deploy.utils.CommandLineUtils
import org.gradle.api.DefaultTask
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.Optional
import org.gradle.api.tasks.TaskAction

/**
 * scp support task
 *
 * @author jyRMc
 * @since 2024/9/14 16:44
 **/
open class ScpTask : DefaultTask() {
    @Input
    @Optional
    lateinit var host: String

    @Input
    var port: Int = 22

    @Input
    var username: String = "root"

    @Input
    @Optional
    lateinit var password: String

    @Input
    @Optional
    lateinit var localFilePath: String

    @Input
    @Optional
    lateinit var uploadPath: String


    @TaskAction
    fun scpAction() {
        // upload file
        val commandLine = "scp -P $port $localFilePath $username@$host:$uploadPath"
        val result = CommandLineUtils.exec(host, port, username, password, commandLine)
        println(result)
    }
}